const Controller = require('egg').Controller;

//解析用户的输入，处理后返回相应的结果

class ListController extends Controller{
    //新增
    async add(){
        let {ctx,service} = this;
        let {timer,title,author='tom',important,num=1,status } = ctx.request.body;
        //参数校验
        if(timer && title && important && status){
            try{
                await service.list.add(timer,title,author,important,num,status);
                ctx.body = {
                    code:1,
                    msg:'添加成功'
                }
            }catch(e){
                ctx.body = {
                    code:0,
                    msg:e
                }
            }
        }else{
            ctx.body = {
                code:2,
                msg:'参数有误'
            }
        }
    }
    //修改
    async update(){
        let {ctx,service} = this;
        let {timer,title,author,important,num,status,id } = ctx.request.body;
        //参数校验
        if(timer && title && author && important && num && status && id){
            try{
                await service.list.update(timer,title,author,important,num,status,id);
                ctx.body = {
                    code:1,
                    msg:'修改成功'
                }
            }catch(e){
                ctx.body = {
                    code:0,
                    msg:e
                }
            }
        }else{
            ctx.body = {
                code:2,
                msg:'参数有误'
            }
        }
    }

    //查询
    async bookslist(){
        let {ctx,service} = this;
        //start ：起始的下标  count：每页的条数
        let {start=0,count=2} = ctx.query;
        let list =await service.list.bookslist(start,count);
        ctx.body = {
            code:1,
            data:list
        }
    }
    //删除
    async del(){
        let {ctx,service} = this;
        let {id} = ctx.query;
        if(id){
            await service.list.del(id);
            ctx.body = {
                code:1,
                msg:'删除成功'
            }
        }else{
            ctx.body = {
                code:2,
                msg:'参数有误'
            }
        }
    }
}

module.exports = ListController